package com.example.orderprovider.dubbo;

import com.example.orderprovider.model.Order;
import com.example.orderprovider.tcc.IOrderAction;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.CouponApiTcc;
import org.example.api.OrderApiTcc;
import org.example.dto.CouponDto;
import org.example.dto.OrderDto;
import org.example.response.R;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

/**
 * @author Kong
 * @date 2021-9-15 15:50
 */
@Slf4j
@DubboService
public class OrderDubboServiceTcc implements OrderApiTcc {
    @Autowired
    private IOrderAction orderAction;
    @DubboReference
    private CouponApiTcc couponApiTcc;
    @Override
    public R createOrder(OrderDto dto) {
        log.info("[TCC模式]全局事务XID:{}",RootContext.getXID());
        Order order = new Order();
        order.setAmount(new BigDecimal(100));
        order.setCount(2);
        order.setOrderNo("20210819000001");
        order.setUserId(1);
        order.setProductCode("20210819000001");
        orderAction.prepare(new BusinessActionContext(),order);
        couponApiTcc.issueCoupon(new CouponDto());
        return R.Builder.getInstance().buildOk().build();
    }
}
